package cn.aaalei.controller;

import cn.aaalei.pojo.ActivityInfo;
import cn.aaalei.pojo.AutoSignAccount;
import cn.aaalei.pojo.SignLogInfo;
import cn.aaalei.pojo.UserInfo;
import cn.aaalei.service.DataSaveService;
import cn.aaalei.service.MainService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main")
@PreAuthorize("hasRole('ADMIN')")
public class MainController {

    @Autowired
    private MainService mainService;

    @Autowired
    private DataSaveService dataSaveService;

    @RequestMapping("/index")
    public String index(Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        Map<String,Object> main = new HashMap<>();
        main.put("havePeople",mainService.getAllPeopleNum());
        main.put("taskNum",mainService.getSignLogNumByStudentNo(userInfo.getStudentNo()));
        main.put("totalNum",mainService.getAllSignLogNum());
        model.addAttribute("loginUser",loginUserMap);
        model.addAttribute("main",main);
        return "index";
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping("/unbind")
    public String unbind(Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        model.addAttribute("loginUser",loginUserMap);
        model.addAttribute("userId",userInfo.getUserId());
        model.addAttribute("studentNo",userInfo.getStudentNo());
        model.addAttribute("name",userInfo.getRealName());
        return "unbind";
    }

    @RequestMapping("/myAttend")
    public String myAttend(Map<String,String> requestMap,Model model) throws Exception{
//        authentication.getName();
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        loginUserMap.put("realName",userInfo.getRealName());
        loginUserMap.put("userId",userInfo.getUserId());
        List<ActivityInfo> activityInfos = mainService.getMyAttend(userInfo.getUserId());
        model.addAttribute("activityList",activityInfos);
        model.addAttribute("loginUser",loginUserMap);
        return "myAttend";
    }

    @RequestMapping("/signLog")
    public String signLog(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Map<String,String> requestMap, Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        model.addAttribute("loginUser",loginUserMap);
//        PageHelper.startPage(pageNum,10);
        List<SignLogInfo> signLogInfos = dataSaveService.findSignLogInfoByStudentNo(userInfo.getStudentNo());
        model.addAttribute("signLogList",signLogInfos);
        return "signLog";
    }

    @RequestMapping("/modify")
    public String modify(Model model,String taskName,String activityId,String taskId) throws Exception {
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        model.addAttribute("loginUser",loginUserMap);
        List noSignList = mainService.getSignHistory(activityId,taskId,userInfo.getUserId());
        model.addAttribute("noSignList",noSignList);
        model.addAttribute("userId",userInfo.getUserId());
        model.addAttribute("studentNo",userInfo.getStudentNo());
        model.addAttribute("name",userInfo.getRealName());
        model.addAttribute("taskName",taskName);
        model.addAttribute("taskId",taskId);
        model.addAttribute("activityId",activityId);
        return "modify";
    }

    @RequestMapping("/pay")
    public String pay(Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        loginUserMap.put("score",userInfo.getScore().toString());
        model.addAttribute("loginUser",loginUserMap);
        return "pay";
    }

    @RequestMapping("/myWallet")
    public String myWallet(Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        loginUserMap.put("score",userInfo.getScore().toString());
        model.addAttribute("loginUser",loginUserMap);
        return "myWallet";
    }

    @RequestMapping("/autoSign")
    public String autoSign(Model model){
        UserInfo userInfo = mainService.getMyProfile(getLoginUser());
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put("username",getLoginUser());
        if (userInfo.getImageUrl() == null){
            userInfo.setImageUrl("../images/logo-sm.png");
        }
        loginUserMap.put("imageUrl",userInfo.getImageUrl());
        model.addAttribute("loginUser",loginUserMap);
        Map<String,String> wisdomMap = new HashMap<>();
        AutoSignAccount account = mainService.getAccount(userInfo.getStudentNo());
        wisdomMap.put("studentNo",userInfo.getStudentNo());
        wisdomMap.put("password",account.getPassword());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String expireTime = "未开通";
        if (!account.getExpireTime().equals("")){
            if(Long.valueOf(account.getExpireTime()) < System.currentTimeMillis()){
                expireTime = "已过期";
            }else {
                expireTime = format.format(new Date(Long.valueOf(account.getExpireTime())));
            }
        }
        wisdomMap.put("expireTime", expireTime);
        model.addAttribute("wisdom",wisdomMap);
        return "autoSign";
    }

    private String getLoginUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getName();
    }
}
